#------------------------------------------------------------------------------
# Project:     Robot Dinosaur Extinction
# Name:        game.py     
# Created:     4/30/2013
# Licence:     New BSD
#------------------------------------------------------------------------------
import pygame, math, sys, random
from dinosaurs import Dinosaur
from scrollingbackground import Background
from Player import Terminator
from Bullet import Bullet
from shotgun import Shotgun
from menu import Button
from health import Healthbar
from screen import Screen
from dinohealth import Dinohealth
#import subprocess
#from subprocess import Popen, PIPE

#cmd = "C:/Windows/System32/cmd C:\Documents and Settings\Student\My Documents\Game Programming\Kevin Cioffi\Terminator\"
#process = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=0x08000000)
#process.wait()




if pygame.mixer:
    pygame.mixer.init()

pygame.init()

clock = pygame.time.Clock()

screenWidth = 800
screenHeight = 600
fullscreen = 0
altFlag = False

dinosaurs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
shotguns = pygame.sprite.Group()

all = pygame.sprite.OrderedUpdates()



Dinosaur.containers = all, dinosaurs
Background.containers = all
Terminator.containers = all
Terminator.bullet = all
Healthbar.containers = all
Dinohealth.containers = all
Bullet.containers = all, bullets
Shotgun.containers = all, shotguns
Terminator.shotgun = all



#if Terminator.Healthbar <= 0:
  #      Terminator.living = False


'''
			elif (event.key == pygame.K_RALT or event.key == pygame.K_LALT):
                    altFlag = True
            elif (event.key == pygame.K_RETURN) and altFlag:
                if fullscreen == 0:
                    fullscreen = pygame.FULLSCREEN
                    else:
                        fullscreen = 0
                    screen = pygame.display.set_mode((screenWidth,screenHeight),
                    							      fullscreen)
                    pygame.display.flip()
'''
screenSize = screenWidth, screenHeight
screen = pygame.display.set_mode(screenSize)
maxDinosaurs = 25
dinosaurList = []
playerBullets = []
playerShotguns = []

background = Screen(["rsc/Player/Player3.png"], [0,0], screenSize, 10)
endscreen = Screen(["rsc/Player/deadscreen.png"], [0,0], screenSize, 10)
singleplayer = Button("Play", [350, 175], (0, 100, 100))
                                #250, 300, 0, 100, 100
exit = Button("Leave", [330,375], (0, 100, 100))
                       #250, 400, 0, 100, 100


bgList = []
for x in range(-2000, 2000, 100):#-2000,2000,100):
    for y in range(-2000,2000,100):
        bgList += [Background([x+50, y+50], screenSize)]
        
player = Terminator([screenWidth/2, screenHeight/2], screenSize)
Healthbar = Healthbar([screenWidth - 75, 30])
                                     #75 #75
bgColor = 0,0,0
run = False

#---------------------Menu-------------------------

while True:
    while not run and player.living:
        #print run, player.living
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_UP or event.key == pygame.K_w) and not singleplayer.highlighted:
                    singleplayer.highlighted = True
                    exit.highlighted = False
                elif event.key == pygame.K_DOWN or event.key == pygame.K_s and not exit.highlighted:
                    exit.highlighted = True
                    singleplayer.highlighted = False
                elif event.key == pygame.K_SPACE and exit.highlighted == True:
                    sys.exit()
                elif event.key == pygame.K_SPACE and singleplayer.highlighted == True:
                    run = True
            elif event.type == pygame.MOUSEMOTION:
                if singleplayer.collidePt(event.pos):
                    singleplayer.highlighted = True
                    exit.highlighted = False
                elif exit.collidePt(event.pos):
                    singleplayer.highlighted = False
                    exit.highlighted = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if singleplayer.collidePt(event.pos):
                        singleplayer.clicked = True
                        run = True
                    elif exit.collidePt(event.pos):
                        exit.clicked = True
                        sys.exit()
            elif event.type == pygame.JOYBUTTONDOWN:
                if event.button == 2: #-- 'X' Button 
                    singleplayer.clicked = True
                    run = True
                elif event.button == 1: #-- 'B' Button 
                    exit.clicked = True
                    sys.exit()
                        

        singleplayer.update((200, 10, 10))
        exit.update((200, 10, 10))
        
        screen.fill(bgColor)
        screen.blit(singleplayer.surface, singleplayer.rect)
        screen.blit(exit.surface, exit.rect)
        pygame.display.flip()


#-----------Move------------------------------

    while run and len(players.sprites()) == 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_UP
                    or event.key == pygame.K_w):
                        player.direction("up")
                elif (event.key == pygame.K_DOWN
                    or event.key == pygame.K_s):
                        player.direction("down")
                elif (event.key == pygame.K_RIGHT
                    or event.key == pygame.K_d):
                        player.direction("right")
                elif (event.key == pygame.K_LEFT
                    or event.key == pygame.K_a):
                        player.direction("left")
                elif (event.key == pygame.K_j):
                    playerBullets += [Bullet(player.rect.center, player.dir, screenSize)]
                elif (event.key == pygame.K_k):
                    playerShotguns += [Shotgun(player.rect.center, player.dir, screenSize)]
                elif (event.key == pygame.K_RALT or event.key == pygame.K_LALT):
                    altFlag = True
                elif (event.key == pygame.K_RETURN) and altFlag:
                    if fullscreen == 0:
                        fullscreen = pygame.FULLSCREEN
                    else:
                        fullscreen = 0
                    screen = pygame.display.set_mode((screenWidth,screenHeight),fullscreen)
                    pygame.display.flip()
                elif (event.key == pygame.K_ESCAPE) and RETURN:
                    sys.exit()
              

                
            elif event.type == pygame.KEYUP:
                if (event.key == pygame.K_UP
                    or event.key == pygame.K_w):
                        player.direction("stop")
                elif (event.key == pygame.K_DOWN
                    or event.key == pygame.K_s):
                        player.direction("stop")
                elif (event.key == pygame.K_RIGHT
                    or event.key == pygame.K_d):
                        player.direction("stop")
                elif (event.key == pygame.K_LEFT
                    or event.key == pygame.K_a):
                        player.direction("stop")
                elif (event.key == pygame.K_RALT or event.key == pygame.K_LALT):
                    altFlag = False
                    
                    
                    
                
                
    
#--------Create Stuff--------------------------------------------
        while len(dinosaurs) < maxDinosaurs:
            dinosaurSpeed = [random.randint(1,6), 
                        random.randint(1,6)]
            dinosaurPos = [random.randint(100,screenWidth-100),
                    random.randint(100,screenHeight/2)]
            dinosaurList += [Dinosaur(dinosaurSpeed, dinosaurPos, screenSize)]
            if random.randint(1,10)  == 1:
                dinopos = [random.randint(100,screenWidth-100),
                    		random.randint(100,screenHeight/2)]
                if player.collide_point(dinopos, 40):
                    dinopos = [random.randint(100,screenWidth-100),
                        random.randint(100,screenHeight/2)]
                    dinosaurs[-1].place(dinopos)
                    insideSomething = True
        
        #print bullets.sprites()
        
        PlayerHitDinosaurs = pygame.sprite.spritecollide(player, dinosaurs, False)
        dinosaursHitDinosaurs = pygame.sprite.groupcollide(dinosaurs, dinosaurs,                    False, False)
    
        bulletsHitDinosaurs = pygame.sprite.groupcollide(bullets, dinosaurs, False, False)
        
        shotgunsHitDinosaurs = pygame.sprite.groupcollide(shotguns, dinosaurs, False, False)
        
        for dinosaur in PlayerHitDinosaurs:
        	 player.dinosaurCollide(dinosaur)
      
        for dinosaur in dinosaursHitDinosaurs:
            for otherDinosaur in dinosaursHitDinosaurs[dinosaur]:
                dinosaur.dinosaurCollide(otherDinosaur)

        for bullet in bulletsHitDinosaurs:
            for dinosaur in bulletsHitDinosaurs[bullet]:
                dinosaur.bulletCollide(bullet)
                print "bulletcolide"
                bullet.kill()
                dinosaur.kill()
                
        for shotgun in shotgunsHitDinosaurs:
            for dinosaur in shotgunsHitDinosaurs[shotgun]:
                shotgun.enemyCollide(dinosaur)
                print "bulletcolide"
                shotgun.kill()
                dinosaur.kill()
        
        
            
           
        
        all.update(player.realx, 
                player.realy, 
                player.scrollingx,
                player.scrollingy,
                player.health,
                player.maxHealth,
                dinosaur.health,
                dinosaur.maxHealth)
                #if run and not player.living:
                    #sys.exit()
        #player.wallCollide()
        #dinosaurs.wallCollide()
        

        #Draw Everything to Screen
    
        #for spt in all.sprites():
            #print spt
    
        dirty = all.draw(screen)
        pygame.display.update(dirty)
        clock.tick(30)
        #print clock.get_fps()
while True:
    while run and len(players.sprites()) == 0:
        sys.exit()
